package com.wdd.studentmanager.service.Impl;

import com.wdd.studentmanager.domain.Score;
import com.wdd.studentmanager.domain.ScoreStats;
import com.wdd.studentmanager.mapper.ScoreMapper;
import com.wdd.studentmanager.service.ScoreService;
import com.wdd.studentmanager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 分页查询成绩
     *
     * @param paramMap 包含分页参数和查询条件
     * @return PageBean<Score> 分页结果
     */
    @Override
    public PageBean<Score> queryPage(Map<String, Object> paramMap) {
        if (paramMap == null || !paramMap.containsKey("pageno") || !paramMap.containsKey("pagesize")) {
            throw new IllegalArgumentException("Invalid parameters for pagination query");
        }

        // 创建分页对象
        PageBean<Score> pageBean = new PageBean<>((Integer) paramMap.get("pageno"), (Integer) paramMap.get("pagesize"));

        // 设置起始索引并查询数据
        paramMap.put("startIndex", pageBean.getStartIndex());
        List<Score> scoreList = scoreMapper.queryList(paramMap);
        pageBean.setDatas(scoreList);

        // 查询总记录数
        Integer totalRecords = scoreMapper.queryCount(paramMap);
        pageBean.setTotalsize(totalRecords);

        return pageBean;
    }

    /**
     * 检查成绩是否及格
     *
     * @param score 成绩对象
     * @return boolean 是否及格
     */
    @Override
    public boolean isScore(Score score) {
        if (score == null) {
            throw new IllegalArgumentException("Score object cannot be null");
        }
        Score retrievedScore = scoreMapper.isScore(score);
        return retrievedScore != null && retrievedScore.getScore() > 60;
    }

    /**
     * 添加成绩
     *
     * @param score 成绩对象
     * @return int 操作结果
     */
    @Override
    public int addScore(Score score) {
        if (score == null) {
            throw new IllegalArgumentException("Score object cannot be null");
        }
        return scoreMapper.addScore(score);
    }

    /**
     * 修改成绩
     *
     * @param score 成绩对象
     * @return int 操作结果
     */
    @Override
    public int editScore(Score score) {
        if (score == null || score.getId() == null) {
            throw new IllegalArgumentException("Score object or ID cannot be null");
        }
        return scoreMapper.editScore(score);
    }

    /**
     * 删除成绩
     *
     * @param id 成绩记录ID
     * @return int 操作结果
     */
    @Override
    public int deleteScore(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("Score ID cannot be null");
        }
        return scoreMapper.deleteScore(id);
    }

    /**
     * 获取所有成绩
     *
     * @param score 查询条件
     * @return List<Score> 成绩列表
     */
    @Override
    public List<Score> getAll(Score score) {
        return scoreMapper.getAll(score);
    }

    /**
     * 获取某课程的平均成绩统计
     *
     * @param courseId 课程ID
     * @return ScoreStats 平均成绩统计
     */
    @Override
    public ScoreStats getAvgStats(Integer courseId) {
        if (courseId == null) {
            throw new IllegalArgumentException("Course ID cannot be null");
        }
        return scoreMapper.getAvgStats(courseId);
    }
}
